<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/15
 * Time: 11:29
 */

namespace App\Repositories;

use App\Models\School;
use App\Models\Service;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class ServiceRepository extends BaseRepository
{

    protected $studenttime;
    protected $select;
    public $class_config;
    public $error;

    public function __construct(
        Service $service,
        SelectRepository $selectRepository,
        ClassConfigRepository $classConfigRepository
    )
    {
        $this->model = $service;
        $this->select = $selectRepository;
        $this->class_config = $classConfigRepository;
    }


    /**入库处理
     * @param $model
     * @param $inputs
     * @return mixed
     */
    protected function save($model, $inputs, $flag=false)
    {
        $model->inscode = Auth::user()->item->inscode;
        if (array_key_exists('inscode',$inputs)) {
            $model->inscode = e($inputs['inscode']);//培训机构编码
        }
        if (array_key_exists('vehicletype',$inputs)) {
            $model->vehicletype = e($inputs['vehicletype']);//培训车型
        }

        if (array_key_exists('trainningmode',$inputs)) {
            $model->trainningmode = e($inputs['trainningmode']);//培训模式
        }

        if (array_key_exists('subiect',$inputs)) {
            $model->subiect = e($inputs['subiect']);//培训部分及方式
        }

        if (array_key_exists('trinningtime',$inputs)) {
            $model->trinningtime = e($inputs['trinningtime']);//培训时段
        }

        if (array_key_exists('chargemode',$inputs)) {
            $model->chargemode = e($inputs['chargemode']);//收费模式
        }

        if (array_key_exists('paymode',$inputs)) {
            $model->paymode = e($inputs['paymode']);//付费模式  1：先学后付  2：先付后学  9：其他
        }
        if (array_key_exists('subscribetype',$inputs)) {
            $model->subscribetype = e($inputs['subscribetype']);//预约模式 1：一车一人 2：一车多人  9：其他
        }
        if (array_key_exists('service',$inputs)) {
            $model->service = e($inputs['service']);//服务内容
        }

        if (array_key_exists('classcurr',$inputs)) {
            $model->classcurr = e($inputs['classcurr']);//服务名称
        }
        //费用
        if (array_key_exists('payprice',$inputs)) {
            $model->payprice = e($inputs['payprice']);//报名费用
        }
        if (array_key_exists('trainningprice',$inputs)) {
            $model->trainningprice = e($inputs['trainningprice']);//培训费用
        }
        if (array_key_exists('examprice',$inputs)) {
            $model->examprice = e($inputs['examprice']);//考试费用
        }
        if (array_key_exists('elseprice',$inputs)) {
            $model->elseprice = e($inputs['elseprice']);//其他费用
        }
        if (array_key_exists('materialprice',$inputs)) {
            $model->materialprice = e($inputs['materialprice']);//材料费
        }
        if (array_key_exists('recordprice',$inputs)) {
            $model->recordprice = e($inputs['recordprice']);//档案材料费
        }
        if (array_key_exists('businessprice',$inputs)) {
            $model->businessprice = e($inputs['businessprice']);//材料业务费
        }
        if (array_key_exists('theoryyprice',$inputs)) {
            $model->theoryyprice = e($inputs['theoryyprice']);//理论知识培训费
        }
        if (array_key_exists('distanceprice',$inputs)) {
            $model->distanceprice = e($inputs['distanceprice']);//远程网络教学
        }
        if (array_key_exists('periodprice',$inputs)) {
            $model->periodprice = e($inputs['periodprice']);//学时培训费
        }
        if (array_key_exists('insurance',$inputs)) {
            $model->insurance = e($inputs['insurance']);//意外伤害保险费
        }
        if (array_key_exists('favorableprice',$inputs)) {
            $model->favorableprice = e($inputs['favorableprice']);//优惠金额
        }
        $model->price = (int)e($inputs['payprice']) + (int)e($inputs['trainningprice']) + (int)e($inputs['examprice']) + (int)e($inputs['elseprice'])
        +(int)e($inputs['materialprice']) + (int)e($inputs['recordprice']) + (int)e($inputs['businessprice']) + (int)e($inputs['theoryyprice'])
        +(int)e($inputs['distanceprice']) + (int)e($inputs['periodprice']) + (int)e($inputs['insurance']) - (int)e($inputs['favorableprice']);//合计金额

        if (array_key_exists('km2period',$inputs)) {
            $model->km2period = e($inputs['km2period']);//科目二学时
        }
        if (array_key_exists('km3period',$inputs)) {
            $model->km3period = e($inputs['km3period']);//科目三学时
        }
        if ($flag) {
            //$model->seq = mt_rand(1000,9999);//收费标准编号
            $model->school_id = \Auth::user()->item_id;//驾校编号
            $seq = Service::where('school_id','=',\Auth::user()->item_id)->max('seq');
            $model->seq = sprintf("%04d", (intval($seq)+1));
        }
        $model->save();
        return $model;

    }

    /**存储收费标准
     * @param $input
     * @return mixed
     */
    public function store($input,$extra = '')
    {
        $model = new $this->model;
        $models =  $this->save($model,$input,true);
        if($models->id){
            //修改权限表数据
            $input['info']['school_id'] = $models->school_id;
            $input['info']['service_id'] = $models->id;
            $this->class_config->store($input['info']);
        }
        return $models;
    }



    /**更新收费标准
     * @param $id
     * @param $input
     * @return mixed
     */
    public function update($id, $input, $extra='')
    {
        $model = $this->getById($id);
        $status =  $this->save($model,$input);
        if($status->id){
            //修改权限表数据
            $input['info']['school_id'] = $status->school_id;
            $input['info']['service_id'] = $status->id;
            if($input['config_id']){//权限表id存在即修改 否则新增权限信息
                $this->class_config->update($input['config_id'],$input['info']);
            }else{
                $this->class_config->store($input['info']);
            }
        }
        return $status;
    }

    /**删除收费标准
     * @param int $id
     * @param string $extra
     * @return int
     */
    public function destroy($id = 0, $extra = '')
    {
        $service = $this->getById($id);
        if ($service->seq) {
            $stats = $this->sapi->deleteCommonJsApi('CharStandard',$service->seq,$service->inscode);
            if ($stats) {
                return $service->delete();
            }else{
                $this->error = $this->sapi->error;
                return false;
            }
        }else{
            $this->error = '收费标准编号不存在';
            return false;
        }

    }


    /**
     * 资源列表
     *
     * @param  array $data 必须传入与模型查询相关的数据
     * @param  string|array $extra 可选额外传入的参数
     * @param  string $size 分页大小（存在默认值）
     * @return Illuminate\Support\Collection
     */
    public function index($data='', $extra='', $size='10')
    {
        $list = $this->model->where('school_id',\Auth::user()->item_id);
        if ($data['traintype']) { //车型
            $list->where('vehicletype', $data['traintype']);
        }
        if ($data['trainningmode']) { //培训模式
            $list->where('trainningmode', $data['trainningmode']);
        }
        if ($data['chargemode']) { //收费模式
            $list->where('chargemode', $data['chargemode']);
        }
        if ($data['paymode']) { // 付费模式
            $list->where('paymode', $data['paymode']);
        }

        return $list->paginate($size);
    }

    /**
     * 获取当前驾校 收费标准列表
     * @return mixed
     */
    public function getSeriveList()
    {
        return $this->model->where('school_id',\Auth::user()->item_id)->select('id','seq','classcurr')->get();
    }

    public function getBygetSeriveArr()
    {
        return $this->model->where('school_id',\Auth::user()->item_id)->lists('classcurr','id')->toArray();
    }
    /**
     * 查询服务项目及收费标准
     * @param $id
     * @return mixed
     */
    public function getByServiveInfo($id)
    {
        $data = $this->model->where('id',$id)->first();
        if($data){

            $data->config = $data ? $this->class_config->getByClassConfigInfo($data->school_id,$data->id) : '';
            return $data;
        }
        return false;
    }

    /**
     * 编辑特定id资源
     *
     * @param  int $id 资源id
     * @param  string|array $extra 可选额外传入的参数
     * @return Illuminate\Support\Collection
     */
    public function edit($id, $extra)
    {
        return true;
    }


}